const jumpInclude = (url, is_gs, gs_flag, bh, xmmc)=>{
  // post("include.do",{mk_dm:val,appname:encodeURI(appname)});
  if (is_gs == 'Y') {
    window.open('/portal/include.do?app_bh=' + bh);
  } else {
    if (gs_flag == 'Y') {
      window.open(url);
    } else {
      window.open('/portal/include.do?sign=' + url.replace(/&/g, '%26') + '&appname=' + encodeURI(xmmc).replace(/%/g, '_'));
    }
    // openMaxWindow(url);
  }
};

const getQueryString = (name)=>{
  var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
  var r = location.search.substr(1).match(reg);
  if (r != null) return unescape(decodeURI(r[2])); return null;
};

const sendSbList = (currentZeroInfo)=>{
  /* let sbAllList=[
    {
      declareName:"增值税小规模纳税人月(季)度申报",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:findIndexButton("增值税小规模纳税人月(季)度申报").txt==="查看",
    },
    {
      declareName:"城建税、教育费附加、地方教育附加税（费）申报表",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:findIndexButton("城建税、教育费附加、地方教育附加税（费）申报表").txt==="查看",
    },
    {
      declareName:"居民企业(查账征收)所得税月（季）申报",
      declareCycle:"month",
      accountBook:"",
      declareTime:"",
      isSuccess:findIndexButton("居民企业(查账征收)所得税月（季）申报").txt==="查看",
    },
  ];
  $(); */
  let sbAllList = getSbList();
  console.log(sbAllList);
  sbAllList.forEach((item, index)=>{
    const date = new Date(),
      year = date.getFullYear(),
      month = date.getMonth() + 1,
      day = date.getDate();
    if (item.declareCycle === 'month') {
      item.accountBook = moment().subtract(1, 'months').format('YYYY-MM');
      item.declareTime = moment().format('YYYY-MM-DD');
    } else if (item.declareCycle === 'year') {
      item.accountBook = moment().subtract(1, 'years').format('YYYY');
      item.declareTime = moment().format('YYYY-MM-DD');
    }
  });

  let backInfo = {
    ...currentZeroInfo,
    recordList: sbAllList
  };
  console.log(backInfo);
  // 零申报结束
  window.postMessage(
    {
      event: 'zeroReportBack',
      data: backInfo
    },
    '*'
  );
};

const getSbList = ()=>{
  return [].slice.call($('#sxtable tr')).map(item=>{
    let startTime = moment($('td:eq(2)', item).html()),
      endTime = moment($('td:eq(3)', item).html());
    let isSuccess = $('.table_btn', item).html() === '查看';
    if ($('td:eq(1)', item).html() === '印花税') {
      let yhs = window.sessionStorage.getItem('yhs');
      if (yhs) isSuccess = true;
    }
    if (startTime.isValid() && endTime.isValid()) {
      return {
        declareName: $('td:eq(1)', item).html(),
        declareCycle: moment($('td:eq(3)', item).html()).diff(moment($('td:eq(2)', item).html()), 'days') < 200 ? 'month' : 'year',
        accountBook: '',
        declareTime: '',
        isSuccess
      };
    }
  }).filter(item=>!!item);
};

const findIndexButton = (word)=>{
  let idx = [].slice.call($('#sxtable tr')).findIndex(item=>{
    return $('td:eq(1)', item).html() === word;
  });

  if (idx > -1) {
    let btn = $('#sxtable tr:eq(' + idx + ') .table_btn');
    return {
      txt: btn.html(),
      btn
    };
  }
  return {
    txt: '',
    btn: null
  };
};

export {
  jumpInclude,
  getQueryString,
  sendSbList,
  findIndexButton
};
